<?php
namespace app\admin\controller;

use app\common\controller\Admin ; 

class Process extends Admin
{
    


    //控制器初始化
    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Process');
    }
    /**
     * 用章流程
     */
    public function index()
    {
        $map = array();
        $map['type_id'] = 1; 

        $searchFieldList = [
            'name' => '流程名字',
        ];
        $searchField = input('searchField/s','');
        $searchValue = input('searchValue/s','');
        if($searchField &&  $searchValue){ $map[$searchField] =['like' , '%' . $searchValue . '%'] ;  }



        $list = $this->model->alias('a')
        ->field('*')
        ->where($map)->paginate(20,false,['query' => request()->param()]);


        $data = array(
            'list' => $list,

            'searchField' => $searchField,
            'searchValue' => $searchValue,
            'searchFieldList' => $searchFieldList,

            );
        $this->assign($data);

        $this->setMeta('系统管理|用章流程管理');
        return view();
    }

    /*维护流程*/
    public function whlist()
    {
        $map = array();
        $map['type_id'] = 2; 

       



        $list = $this->model->alias('a')
        ->field('*')
        ->where($map)->paginate(20,false,['query' => request()->param()]);


        $data = array(
            'list' => $list,
            );
        $this->assign($data);

        $this->setMeta('系统管理|维护流程管理');
        return view();
    }


    public function choosetype(){
        return view('choosetype');
    }

    /**
     * 添加 or  修改
     * @return [type]           [description]
     */
    public function edit(){

        if(IS_AJAX){
            $data   = $this->request->post();
            $data['user_id'] = session('user_auth.user_id');

            //print_r($data);
            if($data['type_id'] == 1 ){
                
                $seal = model('seal')->find($data['seal_id']);
                $data['equipment_id'] = $seal['equipment_id'];
            }else{
                $data['seal_id'] = 0;

            }



            $allowField = true;
            $scene = false;
            if ($data) {
                $result = $this->model
                    ->isUpdate(!!$data['id'])
                    ->validate($scene)
                    ->allowField($allowField )
                    ->save($data);

                if (false === $result) {
                    return $this->error( $this->model->getError() );
                } else {
                    // echo $this->model->getLastSql();
                    return $this->success('更新成功');
                }
            } else {
                return $this->error( '提交数据为空！！！' );
            }  
        }else{

            $id = input('id/d' , 0 );
            $info = $this->model-> get($id); //读取数据 
            $type_id = input('type_id/d' , 1 );

            if($info && $info['type_id']){
                $type_id = $info['type_id'];
            }

            $this->assign('info' , $info );
            $this->assign('type_id' , $type_id );
            $this->setMeta('系统管理|流程管理');
            return view();
        }


        
    }


    /**
     * 删除配置  一个删除 和 多个删除
     *  
     */
    public function del() {

        return $this->error('禁删除！！！');


        $id = array_unique((array) input('id/a', 0));

        if (empty($id)) {
            return $this->error('请选择要删除的数据!');
        }

        $map = array('id' => array('in', $id));

        if ( false === $this->model->where($map)->delete() ) {
            return $this->error('删除失败！' );    // $msg = '', $url = null, $data = '', $wait = 3, array $header = []
        } else {
            return $this->success('删除成功！' );   //$msg = '', $url = null, $data = '', $wait = 3, array $header = []
        }
    }


    /**
     * 申请人 管理
     * @return [type] [description]
     */
    public function apply(){

        $id = input('id/d' , 0 );
        $info = $this->model-> get($id); //读取数据 


        $this->assign('info' , $info );
        $this->setMeta('系统管理|流程管理');
        return view();



    }

    public function applySave(){
        if($this->request->isAjax() ){
            $id = input('process_id/d');
            $ids = input('ids/s');
            $ids = $ids ? explode( ',' , $ids) : [];

            $data = array();
            foreach( $ids as $k => $v){
                $data[]= ['id' => $id, 'role_id' => $v];
            }



            //修改 是 先删除 在 添加 
            // 先删除
            $rs1 = model('ProcessApply')->where('id',$id)->delete();
            // 在添加
            // $rs2 = model('ProcessApply')->insertAll($data);
            $rs2 = model('ProcessApply')->isUpdate(false)
                    ->validate(true)
                    ->allowField(true)
                    ->saveAll($data);

            $this->success('保存成功');



        }

    }

    public function zTreeRole(){
        $process_id = input('process_id/d');


        $Role = model('Role');
        $zNodes = $Role->order('sort asc')->select();
        $zNodes = $Role->zNodes();

        $roles = db('process_apply')->where('id' , $process_id )->column('role_id');
        $roles = implode(',', $roles);
        

        return json(['zNodes'=> $zNodes , 'roles' => $roles ]) ;
        
    }




    public function audit(){
        $id = input('id/d' , 0 );
        $info = $this->model-> get($id); //读取数据 



        $rs = model('process_audit')->getAudits($id);




        $this->assign('listjson' , json_encode($rs) );
        $this->assign('info' , $info );
        $this->setMeta('系统管理|流程管理');
        return view();

    }

    public function auditSave(){

        $post = input('post.');

        $process_id = input('process_id/d',0);
        $steps = input('steps/a',[]);
        

        $model = model('ProcessAudit');
        //先删除
        $rs = $model->where('id',$process_id )->delete();

        //再添加
        $list = array();
        foreach( $steps as $i => $step ){
            // $i++;
            foreach($step as $k => $v){
                $list[] = ['id'=>$post['process_id'], 'step'=>$i, 'role_id'=>$v['role_id']];
            }
        }
        $model->isUpdate(false)->saveAll($list);
        $this->success('保存成功');
    }







}//end class
